#pragma once
#include "Protocol.hpp"
#include <functional>
#include <map>
class NetCal
{
public:
    NetCal() {}
    ~NetCal() {}
    std::shared_ptr<Response> Caculator(std::shared_ptr<Request> req)
    {
        auto resp = make_shared<Response>();
        if (req->Oper() == '/' || req->Oper() == '%')
        {
            if (req->Y() == 0)
            {
                resp->_code = 1;
                if (req->Oper() == '-')
                {
                    resp->_desc = "div zero";
                }
                else
                {
                    resp->_desc = "mod zero";
                }
                return resp;
            }
        }
        map<char, std::function<int(int, int)>> cal;
        cal.insert(std::make_pair('+', [](int x, int y)
                   { return x + y; }));
        cal.insert(std::make_pair('-', [](int x, int y)
                   { return x - y; }));
        cal.insert(std::make_pair('*', [](int x, int y)
                   { return x * y; }));
        cal.insert(std::make_pair('/', [](int x, int y)
                   { return x / y; }));
        cal.insert(std::make_pair('%', [](int x, int y)
                   { return x % y; }));
        if (cal.find(req->Oper()) == cal.end())
        {
            resp->_code = 2;
            resp->_desc = "非法操作";
            return resp;
        }
        resp->_result = cal[req->Oper()](req->X(), req->Y());
        return resp;
    }
};